package com.controller;

import com.bean.*;
import com.mvc.ResponseBody;
import com.service.CourierService;
import com.service.UserService;
import com.util.DateFormatUtil;
import com.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class CourierController {
    CourierService courierService=new CourierService();
    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = courierService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Courier> courierList = courierService.findAll(true, offset, pageNumber);
        List<BootStrapTableCourier> newList = new ArrayList<>();
        for (Courier courier : courierList) {
            String regTime = DateFormatUtil.format(courier.getRegTime());
            String lastLoginTime=courier.getLastLoginTime()==null?"未曾登录":DateFormatUtil.format(courier.getLastLoginTime());
            String cardNumber="******";
            String sum=courier.getSum()>100000?"10w+":courier.getSum().toString();
            BootStrapTableCourier bootStrapTableCourier = new BootStrapTableCourier(courier.getId(),courier.getUserName(),courier.getUserPhone(),cardNumber,courier.getPassword(),sum,regTime,lastLoginTime);
            newList.add(bootStrapTableCourier);
        }
        List<Map<String, Integer>> console = courierService.console();
        Integer total = console.get(0).get("data1_size");
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(newList);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }
    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String cardNumber = request.getParameter("cardNumber");
        String password = request.getParameter("password");
        Courier courier=new Courier(userName,userPhone,cardNumber,password);
        boolean flag = courierService.insert(courier);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("快递员新增成功！");
        } else {
            msg.setStatus(-1);
            msg.setResult("快递员新增失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userPhone");
        //System.out.println(userPhone);
        Courier courier = courierService.findByPhone(userPhone);
        //System.out.println(user.getUserName());
        //System.out.println(express.getUserName());
        Message msg = new Message();
        if (courier == null) {
            msg.setStatus(-1);
            msg.setResult("手机号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(courier);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String userName = request.getParameter("userName");
        String userPhone = request.getParameter("userPhone");
        String cardNumber = request.getParameter("cardNumber");
        String password=request.getParameter("password");
        Courier courier=new Courier();
        courier.setUserName(userName);
        courier.setUserPhone(userPhone);
        courier.setCardNumber(cardNumber);
        courier.setPassword(password);
        boolean flag = courierService.update(id, courier);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = courierService.delete(id);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        } else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
}
